package com.yidu.BusinessParament.service.impl;

import com.google.gson.Gson;
import com.yidu.BusinessParament.entity.SettleMent;
import com.yidu.BusinessParament.mapper.SettleMentMapper;
import com.yidu.BusinessParament.service.SettleMentService;
import com.yidu.CashManagement.entity.FundsAllot;
import com.yidu.CashManagement.service.FundsAllotService;
import com.yidu.CashManagement.util.CreateId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 苏彬
 * @date 2023/2/8 8:54
 **/
@Service
public class SettleMentImpl implements SettleMentService {
    @Autowired
    private SettleMentMapper mentMapper;

    @Autowired
    FundsAllotService fundsAllotService;

    /**
     * 查询所有交易数据
     * @param page 当前页
     * @param row 显示页
     * @param status 状态
     * @param dealTime 时间
     * @param dealWay 交易方式
     * @return
     */
    @Override
    public Map selectSettleMent(int page, int row, String status, String dealTime, String dealWay) {
        //创建空map
        Map selectMap = new HashMap();
        //拼接条件
        StringBuffer term = append(status, dealTime, dealWay);
        //传入当前页
        selectMap.put("page", (page - 1) * row);
        //传入显示页
        selectMap.put("row", row);
        //传入条件
        selectMap.put("term", term);

        //创建空map
        Map layUiMap = new HashMap();
        //传入date数据集合
        layUiMap.put("data", mentMapper.selectSettleMent(selectMap));
        //传入msg
        layUiMap.put("msg", "");
        //传入code
        layUiMap.put("code", 0);
        //传入显示行数
        layUiMap.put("count", 10);
        //返回layuiMap
        return layUiMap;
    }

    /**
     * 修改交易结算状态
     * @param myJson json格式
     * @param flag 条件 false 为未结算   反则为已结算
     * @return int 执行行数
     */
    @Override
    public int updateSettleMent(String myJson, boolean flag) {
        //定义执行行数
        int i = 0;
        //用Gson转为SettleMent数组
        SettleMent[] settlement = new Gson().fromJson(myJson, SettleMent[].class);
        //flge为false
        if (!flag) {
            //循环settlement数组
            for (SettleMent ment : settlement) {
                //创建map集合
                Map map = new HashMap();
                //设置dealState状态 flag为true则0 否1
                map.put("dealState", flag ? 0 : 1);
                //传入dealId
                map.put("dealId", ment.getDealId());
                //修改交易数据
                i = mentMapper.updateSettleMent(map);
                //创建空FundsAllot
                FundsAllot fundsAllot = new FundsAllot();
                //赋值逆向编号  交易Id
                fundsAllot.setBusinessId(ment.getDealId());
                //赋值资金调拨Id用工具类创建
                fundsAllot.setFundsAllotId(CreateId.createId("FA"));
                //赋值调拨金额
                fundsAllot.setAllotMoney(ment.getEndMoney());
                //赋值账户名称
                fundsAllot.setAccountName(ment.getAccountName());
                //赋值流入流出方向  买入为1 则-1 否 1
                fundsAllot.setAllotDirection(ment.getDealWay() == 1 ? -1 : 1);
                //赋值 交易时间
                fundsAllot.setBusinessTime(ment.getDealTime());
                //赋值 结算时间
                fundsAllot.setAllotTime(ment.getEndTime());
                //赋值基金Id
                fundsAllot.setFundId("159934");
                //赋值现金Id
                fundsAllot.setCashId(ment.getCashId());
                //赋值交易类型
                fundsAllot.setAllotType(3);
                //赋值备注
                fundsAllot.setRemark("");
                //执行添加add方法
                fundsAllotService.addFundsAllot(fundsAllot);

            }
        } else {
            //循环settlement
            for (SettleMent ment : settlement) {
                //创建空map
                Map map = new HashMap();
                //传入dealState flag true 0  否   1
                map.put("dealState", flag ? 0 : 1);
                //传入交易Id
                map.put("dealId", ment.getDealId());
                //修改交易状态
                i = mentMapper.updateSettleMent(map);
                //资金调拨执行删除
                fundsAllotService.deleteFundsAllotBusinessId(ment.getDealId());
            }
        }
        //返回执行行数
        return i;
    }

    /**
     * 拼接添加
     * @param status 状态
     * @param dealTime 交易数据
     * @param dealWay 交易方式
     * @return StringBuffer
     */
    public StringBuffer append(String status, String dealTime, String dealWay) {
        //创建空StringBuffer
        StringBuffer term = new StringBuffer();
        //判断status 是否为空 拼接
        if (status != null && !status.equals("")) {
            term.append(" and d.`dealState`='").append(status).append("'");
        }
        //判断dealTime 是否为空 拼接
        if (dealTime != null && !dealTime.equals("")) {
            term.append(" and d.`dealTime`='").append(dealTime).append("'");
        }
        //判断dealWay 是否为空 拼接
        if (dealWay != null && !dealWay.equals("")) {
            term.append(" and d.`dealWay`='").append(dealWay).append("'");
        }
        //返回term
        return term;
    }
}
